<script setup lang="ts">
import AddWrite from './components/addWrite.vue'
import { ContentDetailWrap } from '@/components/ContentDetailWrap'
import { ref, unref } from 'vue'
import { ElButton } from 'element-plus'
import { useI18n } from '@/hooks/web/useI18n'
import { useRouter } from 'vue-router'
import { customs_orderAdd } from '@/api/common'
import { CustomsClearanceFormType } from '@/api/common/types'
import { useEmitt } from '@/hooks/web/useEmitt'

const { emitter } = useEmitt()

const { push } = useRouter()

const { t } = useI18n()

const addWrite = ref<ComponentRef<typeof AddWrite>>()

const loading = ref(false)

const save = async () => {
  const write = unref(addWrite)
  console.log('---addWrite---', write?.getFormData())

  let goods: any = []

  write?.getTableData().forEach((item) => {
    let goodsItem: any = {}
    goodsItem.waybill = item.waybill
    goodsItem.batchno = item.batchno // 批次号
    goodsItem.customs_name = write?.getFormData()?.customs_id // 清关公司名称
    goodsItem.export_weight = item.export_weight // 转出重量
    goodsItem.export_volume = item.export_volume // 转出体积
    goodsItem.export_density = item.export_density // 转出密度
    goodsItem.export_value = item.export_value // 转出货值
    goodsItem.export_insure = item.export_insure // 转出保险
    goodsItem.export_other_fund = item.export_other_fund // 转出其它费
    goodsItem.customs_price = item.customs_price // 清关价格
    goodsItem.customs_money = item.customs_money // 清关金额
    goodsItem.export_transport = item.export_transport // 转出运输方式
    goodsItem.margin = item.margin // 毛利润
    goodsItem.internal_charge = item.internal_charge // 国内费用
    goodsItem.pledge = item.pledge // 押金
    goodsItem.remark2 = item.remark2 // 商标备注
    goodsItem.foreign_charge = item.foreign_charge // 国外费用
    goodsItem.houcheng = item.houcheng // 后程不可回收

    console.log('---goodsItem----', goodsItem, item)

    goods.push(goodsItem)
  })

  const data = {
    id: write?.getFormData()?.id,
    code: write?.getFormData()?.code,
    batchno: write?.getFormData()?.batchno,
    customs_id: write?.getFormData()?.customs_id,
    quantity: write?.getFormData()?.quantity,
    density: write?.getFormData()?.density,
    units: write?.getFormData()?.units,
    export_weight: write?.getFormData()?.export_weight,
    export_volume: write?.getFormData()?.export_volume,
    export_density: write?.getFormData()?.export_density,
    total_fee: write?.getFormData()?.total_fee,
    price: write?.getFormData()?.price,
    mobile: write?.getFormData()?.mobile,
    worker: write?.getFormData()?.worker,
    margin: write?.getFormData()?.margin,
    internal_charge: write?.getFormData()?.internal_charge,
    pledge: write?.getFormData()?.pledge,
    foreign_charge: write?.getFormData()?.foreign_charge,
    goods: goods
  } as CustomsClearanceFormType
  // data.buytime = '' + parseInt(data.buytime) / 1000
  console.log('---客户新增参数---', data)

  for (let key in data) {
    if (data[key] == '') {
      delete data[key]
    }
  }

  const res = await customs_orderAdd(data)
  if (res) {
    emitter.emit('getList', 'add')
    push('/sysParameter/CustomsClearanceFormCompany')
  }
}
</script>

<template>
  <ContentDetailWrap :title="t('exampleDemo.add')" @back="push('/logistics/CustomsClearanceForm')">
    <!-- <Write ref="writeRef" /> -->
    <AddWrite ref="addWrite" />

    <template #right>
      <ElButton type="primary" :loading="loading" @click="save">
        {{ t('exampleDemo.save') }}
      </ElButton>
    </template>
  </ContentDetailWrap>
</template>
